from django.contrib import admin
from django.utils.safestring import mark_safe

import info.models
from .models import *

admin.site.site_header = '体院信息管理'
admin.site.site_title = '体院信息管理'
admin.site.index_title = '体院信息管理'


# Register your models here.
@admin.register(Activity)
class ActivityAdmin(admin.ModelAdmin):
    list_display = ('name', 'create_time', 'status', 'description')
    list_editable = ('status',)
    search_fields = ('name',)
    list_filter = ('status',)


@admin.register(ActLog)
class ActivityAdmin(admin.ModelAdmin):
    list_display = ('student', 'activity', 'upload_img', 'create_time')
    search_fields = ('student__name',)
    list_filter = ('student__cla__ins', 'activity', 'student__cla')
    list_per_page = 10

    def upload_img(self, obj):
        try:
            img = mark_safe('<img src="%s" width="200px" />' % (obj.img_url,))
        except Exception as e:
            img = ''
        return img


@admin.register(NoLog)
class NoLog(admin.ModelAdmin):
    list_display = ("act_name", "ins_name", "classname", "name", "phone", "dorm")
    search_fields = ('name',)
    list_filter = ('act_name', "ins_name", 'classname', "dorm")
    list_per_page = 25
    actions = ['mark_done']

    def mark_done(self, request, queryset):
        for i in queryset:
            cla = info.models.Class.objects.get(classname=i.classname)
            student = info.models.Student.objects.get(name=i.name, cla=cla)
            act = Activity.objects.get(name=i.act_name)
            ActLog.objects.create(student=student, activity=act)

    mark_done.short_description = '标记已完成'